/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package YottaTron.Sprites;

import YottaTron.*;
import java.awt.Point;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Iterator;

/**
 *
 * @author Cenda
 */
public class ProjectileSprite extends Sprite {

    int damage;
    
    private Runner runner;
    public GraphicsLoader projectileGraphics;
    public AnimatedGraphicsLoader hitAnimation;
    public boolean incendiary;

    public ProjectileSprite(int shooterID, double x, double y, int width, int height, double realDirection, int rychlost, GraphicsLoader projectileGraphics, AnimatedGraphicsLoader hitAnimation,  int damage, boolean incendiary, Runner runner) {
        super(x, y, width, height, shooterID);
        this.damage = damage;
        this.runner = runner;
        this.projectileGraphics = projectileGraphics;
        setSpeed(rychlost);
        changeDirection(realDirection);
       
        //System.out.println("projectile speed" + getSpeed() + ", dx/dy " + getDx() + "/" + getDy());
        this.hitAnimation = hitAnimation;
        this.incendiary = incendiary;
        hitAnimation.alpha = 0.9f;
    }

    @Override
    public void handleCollision(Sprite target) {
        
       
        if (target != null){
            
            if (target.ID != ID){
               //System.out.println("strela koliduje");
             this.setSpeed(0);
             removeAllActions();
             if(target.ID>=10000){
               ((ObstacleSprite)target).damage(damage,incendiary );
             }else if(target.ID>1000){
               ((PlayerSprite)target).damage(damage,this);
             }else{
              target.damage(damage);

             }
             destroySprite(runner);
            }
        }
    }

    @Override
    public void destroySprite(Runner runner) {
        active = false;
        runner.removeProjectile(this);
        runner.dePopulate(this);
        runner.ShowAnimationAt(new ImagesPlayer(hitAnimation,0.1,1,runner.globalTickTime,false,runner), (int)getX(), (int)getY());

    }

    @Override
    public ArrayList<Point2D> getShape() {
        return projectileGraphics.getShape();
    }

    //zjisti na kterych polickach herniPole se sprite nachazi
    @Override
    public ArrayList<Point2D> getOccupiedCells(){
    ArrayList<Point2D> occupiedCells = new ArrayList<Point2D>();
        for (Iterator<Point2D> it = projectileGraphics.getShape().iterator(); it.hasNext();) {
            Point2D point = it.next();
            occupiedCells.add(new Point(runner.XToTiles((int) (point.getX() + getX())),runner.YToTiles((int) (point.getY() + getY()))));
        }
    return occupiedCells;
    }

}
